package com.bio.action.call;

import java.util.Calendar;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.bio.action.AbstractAction;
import com.bio.entity.call.Call;
import com.bio.entity.call.History;
import com.bio.service.call.CallService;
import com.bio.service.call.HistoryService;
import com.bio.utils.Environment;

@Namespace("/call")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/call/listDetail.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "history!list", params={"callId","${callId}"}, type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/call/editHistory.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/call/addHistory.jsp", type = "dispatcher")
		})
public class HistoryAction extends AbstractAction<History, HistoryService> {

	public HistoryAction(HistoryService service) {
		super(service);
	}

	Logger logger = Logger.getLogger(HistoryAction.class);
	
	private static final long serialVersionUID = 1767901100207478807L;

	private CallService callService;
	
	private Long callId;
	
	private Call call;
	
	private History history;

	public String list(){
		this.call = this.callService.get(callId);
		pagination.setCurrentIndex(this.getCurrentIndex());
		this.pager = this.service.findPagerByCallId(pagination, callId);
		return "list";
	}
	
	public String add(){
		return "add";
	}
	
	public String edit(){
		this.history = this.service.get(id);
		return "edit";
	}
	
	public String deleteAll(){
    	for(int i = 0; i < ids.length; i++){
    		if(i == 0){
    			this.callId = this.service.get(ids[i]).getCallId();
    		}
    		this.service.remove(ids[i]);
    	}
    	return "tolist";
    }
	
	public String save(){
		Calendar now = Calendar.getInstance();
		this.history.setCreatedDate(now);
		this.history.setInchargeUserName(Environment.Session.getUserName());
		if(null == this.history.getRecordDate()){
			this.history.setRecordDate(now);
		}
		this.service.save(this.history);
		return "tolist";
	}
	
	public String update(){
        History old = this.service.get(history.getId());
		
		this.history.setUpdatedDate(Calendar.getInstance());
		this.history.setCreatedDate(old.getCreatedDate());
		
		if(null == this.history.getRecordDate()){
			this.history.setRecordDate(old.getRecordDate());
		}
		
		this.service.save(history);
		return "tolist";
	}
	
	public Call getCall() {
		return call;
	}

	public void setCall(Call call) {
		this.call = call;
	}

	public History getHistory() {
		return history;
	}

	public void setHistory(History history) {
		this.history = history;
	}

	public void setCallService(CallService callService) {
		this.callService = callService;
	}

	public Long getCallId() {
		return callId;
	}

	public void setCallId(Long callId) {
		this.callId = callId;
	}
}
